package com.leetcode.string;

/**
 * @author Dennis Li
 * @date 2021/1/4 15:02
 */
public class Convert_6 {

    public static String convert(String s, int numRows) {
        if (numRows == 1) return s;

        StringBuilder[] sbs = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            sbs[i] = new StringBuilder();
        }

        int pos = 0;
        boolean upstair = false;
        while (pos < s.length()) {
            int index = !upstair ? 0 : numRows - 1;
            if (!upstair) {
                while (index < numRows - 1 && pos < s.length()) {
                    char c = s.charAt(pos);
                    sbs[index].append(c);
                    index++;
                    pos++;
                }
            } else {
                while (index > 0 && pos < s.length()) {
                    char c = s.charAt(pos);
                    sbs[index].append(c);
                    index--;
                    pos++;
                }
            }
            upstair = !upstair;
        }

        StringBuilder res = new StringBuilder();

        for (int i = 0; i < numRows; i++) {
            res.append(sbs[i]);
        }

        return res.toString();
    }

}
